//
//  UIImageView+webImage.swift
//  飞哥大微博
//
//  Created by huangfei on 2017/6/11.
//  Copyright © 2017年 huangfei. All rights reserved.
//

import SDWebImage

extension UIImageView {
    
    /// 隔离 SDWebImage 设置图像函数
    ///
    /// - Parameters:
    ///   - urlString: URLString
    ///   - placeholderImage: 占位图像
    ///   - isAvatar: 是否头像
    func fg_setImage(urlString:String?,placeholderImage:UIImage?,isAvatar:Bool = false) {
        
        // 处理 URL
        guard let urlString = urlString,
            let url = URL(string: urlString) else{
            // 设置占位图像
            image = placeholderImage
            
            return
        }
        sd_setImage(with: url, placeholderImage: placeholderImage, options: [], progress: nil) { [weak self] (image, _, _, _) in
            // 完成回调，判断是否是头像
            if isAvatar {
                self?.image = image?.fg_avatarImage(size: self?.bounds.size)
            }
        }
    }
}
